package service

import (
	"gin_oj/define"
	"gin_oj/model"
	"log"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

// GetSubmitList
// @Tags 公共方法
// @Summary 提交列表
// @Param page query int false "page"
// @Param size query int false "size"
// @Param problem_identity query string false "problem_identity"
// @Param user_identity query string false "user_identity"
// @Param status query int false "status"
// @Success 200 {string} json "{"code":"200","data":""}"
// @Router /submit-list [get]
func GetSubmitList(c *gin.Context) {
	page, err := strconv.Atoi(c.DefaultQuery("page", define.DefaultPage))
	if err != nil {
		log.Fatal("get page err: ", err)
	}
	size, err := strconv.Atoi(c.DefaultQuery("size", define.DefaultSize))
	if err != nil {
		log.Fatal("get size err: ", err)
	}
	userIdentity := c.Query("user_identity")
	problemIdentity := c.Query("problem_identity")
	status, err := strconv.Atoi(c.Query("status"))

	var count int64
	list := make([]*model.SubmitBasic, 0)
	tx := model.GetSubmitList(problemIdentity, userIdentity, status)
	err = tx.Count(&count).Offset((page - 1) * size).Limit(size).Find(&list).Error
	if err != nil {
		log.Fatal("get submit list error: ", err)
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"data": gin.H{
			"list":  list,
			"count": count,
		},
	})
}
